/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package domain.threading;

import domain.DomainController;
import domain.settings.Settings;
import java.util.logging.Level;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import persistence.net.Client;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 * Fetches movie information from the internet, using a IMDB API.
 */
public class MovieInfo extends Thread
{
    private String movieName;

    /**
     * Constructor.
     * @param movieName The movie name.
     */
    public MovieInfo(String movieName)
    {
        this.movieName = movieName;
    }

    @Override
    public void run()
    {
        try
        {
            String rawMovieInfo = null;

            Client client = new Client();
            client.doGet("http://www.imdbapi.com/?i=&t=" + movieName.replaceAll(" ", "+"));

            rawMovieInfo = client.getResponseContent();
            JSONObject json = (JSONObject)JSONSerializer.toJSON(rawMovieInfo);

            /*
             * SAMPLE
             *
                {
                    "Title":"Sin City",
                    "Year":"2005",
                    "Rated":"Unrated",
                    "Released":"1 Apr 2005",
                    "Genre":"Crime, Drama, Thriller",
                    "Director":"Frank Miller, Robert Rodriguez",
                    "Writer":"Frank Miller",
                    "Actors":"Mickey Rourke, Clive Owen, Bruce Willis, Jessica Alba",
                    "Plot":"A film that explores the dark and miserable town, Basin City, and tells the story of three different people, all caught up in violent corruption.",
                    "Poster":"http://ia.media-imdb.com/images/M/MV5BMTI2NjUyMDUyMV5BMl5BanBnXkFtZTcwMzU0OTkyMQ@@._V1._SX320.jpg",
                    "Runtime":"2 hrs 27 mins",
                    "Rating":"8.3",
                    "Votes":"270944",
                    "ID":"tt0401792",
                    "Response":"True"
                }
             *
             */

            //We want: year, genre, actors, runtime, plot
            try
            {
                String year = json.getString("Year");
                String genre = json.getString("Genre");
                String actors = json.getString("Actors");
                String runtime = json.getString("Runtime");
                String plot = json.getString("Plot");

                String result = "[quote]" + Settings.LINE_SEPARATOR;
                result += "[b]Year:[/b] " + year + Settings.LINE_SEPARATOR;
                result += "[b]Genre:[/b] " + genre + Settings.LINE_SEPARATOR;
                result += "[b]Actors:[/b] " + actors + Settings.LINE_SEPARATOR;
                result += "[b]Runtime:[/b] " + runtime + Settings.LINE_SEPARATOR;
                result += "[b]Plot:[/b] " + plot + Settings.LINE_SEPARATOR;
                result += "[/quote]";

                DomainController.getInstance().getLogger().log(Level.INFO, "Info fetched for movie \"{0}\".", movieName);

                DomainController.getInstance().getUi().insertTagInPostBody(result);
            }
            catch(Exception e)
            {
                //No movie info...
                DomainController.getInstance().getUi().setStatusIndeterminate(false, "Done");
                DomainController.getInstance().getUi().setStatusIndeterminate(false, "Done");
                String err = "No info available for movie ";
                DomainController.getInstance().getLogger().log(Level.INFO, err + "\"{0}\".", movieName);
                DomainController.getInstance().fireMessage(err + movieName + ".", "Get movie info");
            }
        }
        catch(Exception e)
        {
            DomainController.getInstance().getLogger().log(Level.SEVERE, "Error while trying to get movie info. {0}", e.getMessage());
        }
        finally
        {
            DomainController.getInstance().getUi().setButtonsEnabled(true);
            DomainController.getInstance().getUi().setStatusIndeterminate(false, "Done");
        }
    }
}